package com.ideal.frame.node.netty.server;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Created by haoxy on 2018/10/17.
 * E-mail:hxyHelloWorld@163.com
 * github:https://github.com/haoxiaoyong1014
 */
@Component
public class NettyServerInitializer extends ChannelInitializer<Channel> {
    @Autowired
    NettyServerHandle nettyServerHandle;

    @Value("${netty.server.read-frequency:5}")
    private int readerIdleTimeSecond;

    @Override
    protected void initChannel(Channel channel) throws Exception {
        channel.pipeline()
                //五秒没有收到消息 将IdleStateHandler 添加到 ChannelPipeline 中
                .addLast(new IdleStateHandler(readerIdleTimeSecond, 0, 0))
                .addLast(new NettyServerDecoder())
                .addLast(nettyServerHandle);
    }
}
